<? require_once("sitemap.php");

$pricesale=0;
$pricesalemembers=0;
$pricepurchase=0;
$pricepurchasemembers=0;
$action="start";

?>
<html>
	<title><? echo (":" . $HTTP_SERVER_VARS['REMOTE_USER'] . ": ". $Botname); ?>'s EL-BOT Pricelist Management</title>
	<script type="text/javascript">
	function checkchanges()
	{
		
		return true;
	}
	</script>
<body>
	<h1><? echo ($Botname); ?>'s EL-BOT Pricelist Management</h1>

<?
if(isset($_POST['action']))
{
	$action = $_POST['action'];
	echo "Action: $action";
	$id=intval(mysql_real_escape_string($_POST['id']));

	$pricesale=floatval(mysql_real_escape_string($_POST['pricesale']));
	$pricesalemembers=floatval(mysql_real_escape_string($_POST['pricesalemembers']));
	$pricepurchase=floatval(mysql_real_escape_string($_POST['pricepurchase']));
	$pricepurchasemembers=floatval(mysql_real_escape_string($_POST['pricepurchasemembers']));
}

if($action == "update" || $action == "insert")
{
	if($pricesale<$pricepurchase || $pricesale<$pricepurchasemembers || $pricesalemembers<$pricepurchase || $pricesalemembers<$pricepurchasemembers)
	{
		if($pricesale<$pricepurchase)
		{
			echo "Error - sale price is less than purchase price<br>";
		}
		if($pricesalemembers<$pricepurchase)
		{
			echo "Error - member sale price is less than purchase price<br>";
		}
		if($pricesale<$pricepurchasemembers)
		{
			echo "Error - sale price is less than member purchase price<br>";
		}
		if($pricesalemembers<$pricepurchasemembers)
		{
			echo "Error - member sale price is less than member purchase price<br>";
		}
	}
	else
	{
		$result = mysql_query("SELECT id FROM pricelist WHERE botid = '$botid' AND knownitemsid = '$id';",$db);
		
		if (mysql_num_rows($result)>0)
		{
			$myrow = mysql_fetch_row($result);
			$pricelistid = $myrow[0];
			$sql = "UPDATE pricelist SET pricesale='$pricesale', pricesalemembers='$pricesalemembers', pricepurchase='$pricepurchase', pricepurchasemembers='$pricepurchasemembers' WHERE id='$pricelistid';";
			$result = mysql_query($sql,$db);
			echo (mysql_error());
		}
		else
		{
			
			$sql = "INSERT INTO pricelist (knownitemsid, botid, pricesale, pricesalemembers, pricepurchase, pricepurchasemembers) VALUES('$id','$botid', '$pricesale','$pricesalemembers','$pricepurchase','$pricepurchasemembers');";
			echo $sql;
			$result = mysql_query($sql,$db);
			echo (mysql_error());
		}
	}
}

?>

	<h2>Legend:</h2>
	<table border="1" cellpadding="0" cellspacing="0">
	<tr>
			<td bgcolor="Red">No prices were set</td>
			<td bgcolor="Yellow">Only sale price is set</td>
			<td bgcolor="Blue">Only purchase price is set</td>	
			<td bgcolor="Green">sale price and purchase price are set</td>
		</tr>
	</table>
	<h2>Bots price list:</h2>
	<table border="1" cellpadding="0" cellspacing="0">
		<tr bgcolor="Gray">
			<td>id</td>
			<td>name</td>
			<td>sale</td>
			<td>sale members</td>
			<td>purchase</td>
			<td>purchase members</td>
			<td>Functions</td>
		</tr>
		<?
			$result = mysql_query("SELECT id, name FROM knownitems WHERE imageid!=3 ORDER BY name",$db);
			while ($myrow = mysql_fetch_row($result))
			{
				$resultB = mysql_query("SELECT id, pricesale, pricesalemembers, pricepurchase, pricepurchasemembers  FROM pricelist WHERE botid = '$botid' AND knownitemsid = '$myrow[0]'",$db);
				echo mysql_error();
				if (mysql_num_rows($resultB)>0)
				{
					$myrowB = mysql_fetch_row($resultB);
					$pricesale=$myrowB[1];
					$pricesalemembers=$myrowB[2];
					$pricepurchase=$myrowB[3];
					$pricepurchasemembers=$myrowB[4];
				}
				else
				{
					$pricesale=0;
					$pricesalemembers=0;
					$pricepurchase=0;
					$pricepurchasemembers=0;
				}
			
				if ($pricesale == 0 && $pricepurchase != 0)
				{
					echo ('<tr bgcolor="Blue">');
				}				
				if ($pricesale != 0 && $pricepurchase == 0)
				{
					echo ('<tr bgcolor="Yellow">');
				}
				if ($pricesale != 0 && $pricepurchase != 0)
				{
					echo ('<tr bgcolor="Green">');
				}
				if ($pricesale == 0 && $pricepurchase == 0)
				{
					echo ('<tr bgcolor="Red">');
				}
		?>
				<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
				<input type="hidden" name="action" value="update">
				<input type="hidden" name="id" value="<?echo ($myrow[0])?>">
				<td><?echo ($myrow[0])?></td>
				<td><?echo ($myrow[1])?></td>
				<td><input type="text" size=12 name="pricesale" value="<?echo ($pricesale)?>"></td>
				<td><input type="text" size=12 name="pricesalemembers" value="<?echo ($pricesalemembers)?>"></td>
				<td><input type="text" size=12 name="pricepurchase" value="<?echo ($pricepurchase)?>"></td>
				<td><input type="text" size=12 name="pricepurchasemembers" value="<?echo ($pricepurchasemembers)?>"></td>
				<td><input type="submit" name="submit" value="UPDATE"></td>
				</form>
		</tr>
		<?
			}
		?>
	</table>
</body>
</html>
